﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using SpellCaster;

namespace SpellCaster3.Core.Importer
{
    public static class ImportManager
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="path">Path to .sav profile</param>
        /// <returns></returns>
        public static Tuple<Profile, string> Import(string path)
        {
            Profile tmp = new Profile();

            if (!path.ToLower().EndsWith(".sav")) throw new FormatException("Il file che si sta tentando di importare non è valido");

            FileStream stream = null;
            SetEntry oldSet;
            try
            {
                stream = new FileStream(path, FileMode.Open);
                IFormatter formatter = new BinaryFormatter();

                oldSet = (SetEntry)formatter.Deserialize(stream);
                stream.Close();
            }
            catch (Exception excp)
            {
                if (stream != null) stream.Close();
                throw excp;
            }

            StringBuilder sb = new StringBuilder();
            // Conversion
            foreach (object obj in oldSet.List)
            {
                SpeechEntry spc = obj as SpeechEntry;

                if (IconsReference[spc.Icon] == InvalidIcon)
                    sb.Append(spc.Name + ", ");

                tmp.ButtonsMacro.Add(new ButtonMacro()
                {
                    Name = spc.Name,
                    Alias = spc.Alias,
                    Command1 = spc.Speech,
                    Command2 = spc.Speech2,
                    Position = spc.Location,
                    HueIndex = spc.Hue + 1,
                    GumpIndex = IconsReference[spc.Icon] == InvalidIcon ? Forms.GumpPicker.BaseRange.Minimum : IconsReference[spc.Icon],
                });
            }

            string retString = sb.ToString();
            if (!string.IsNullOrWhiteSpace(retString))
                return new Tuple<Profile, string>(tmp, retString.Substring(0, retString.Length - 2));
            else
                return new Tuple<Profile, string>(tmp, string.Empty);
        }

        #region Icons Reference
        public const int InvalidIcon = -1;

        public static Dictionary<int, int> IconsReference = new Dictionary<int, int>()
        {
            {0,InvalidIcon},
            {1,InvalidIcon},
            {2,InvalidIcon},
            {3,InvalidIcon},
            {4,InvalidIcon},
            {5,InvalidIcon},
            {6,InvalidIcon},
            {7,InvalidIcon},
            {8,InvalidIcon},
            {9,2265},
            {10,20488},
            // Demone su circolo
            {11,InvalidIcon},
            {12,2419},
            {13,2298},
            {14,20740},
            // Spada barlume
            {15,InvalidIcon},
            // Piccola armatura splendente
            {16,InvalidIcon},
            {17,1264},
            {18,2271},
            {19,2284},
            // Sferetta nera + pietra
            {20,InvalidIcon},
            {21,2256},
            {22,20737},
            {23,1402},
            {24,2245},
            {25,2274},
            {26,20743},
            {27,20745},
            {28,20487},
            {29,20999},
            // CHECKPOINT: Vulcano che erutta
            {30,1406},
            {31,2410},
            {32,20742},
            {33,2243},
            {34,2268},
            {35,2266},
            {36,2411},
            {37,2244},
            {38,2247},
            {39,2300},
            {40,2303},
            {41,2299},
            {42,2302},
            {43,2406},
            {44,1415},
            {45,2301},
            {46,2407},
            // Angelo nero con ali
            {47,InvalidIcon},
            // Faccia da leone
            {48,InvalidIcon},
            // Unicorno
            {49,InvalidIcon},
            // Aquila
            {50,InvalidIcon},
            // Pantera che salta
            {51,InvalidIcon},
            {52,1273},
            // Fiamma malvagia che ride
            {53,InvalidIcon},
            // Scheletro in fiamme
            {54,InvalidIcon},
            // Profilo d'aquila
            {55,InvalidIcon},
            {56,2282},
            // Evoca elfa
            {57,InvalidIcon},
            // Evoca arcere
            {58,InvalidIcon},
            {59,2280},
            {60,2293},
            {61,2257},
            {62,2290},
            {63,2269},
            {64,2288},
            {65,2289},
            {66,2281},
            // CHECKPOINT: 2 Teschi con fiamma in mezzo
            {67,1403},
            {68,2240},
            // Evoca elementale di terra
            {69,InvalidIcon},
            {70,2412},
            {71,2250},
            {72,2283},
            {73,2272},
            {74,20484},
            // Notte con stelle
            {75,InvalidIcon},
            {76,20491},
            // Martello
            {77,InvalidIcon},
            {78,2285},
            {79,2273},
            {80,20482},
            {81,20481},
            {82,2263},
            {83,2295},
            {84,20485},
            {85,2267},
            {86,1412},
            {87,2408},
            {88,2264},
            // Sfera con esplosione all'interno
            {89,InvalidIcon},
            {90,21257},
            {91,2242},
            {92,2270},
            {93,2249},
            {94,2292},
            {95,20495},
            {96,20494},
            // CHECKPOINT: Urlo ankh
            {97,1404},
            {98,20736},
            {99,2248},
            // Occhio chiuso
            {100,InvalidIcon},
            {101,2241},
            {102,21008},
            {103,1414},
            {104,2246},
            {105,2279},
            {106,20739},
            {107,20744},
            {108,2414},
            {109,2294},
            {110,20492},
            {111,2291},
            {112,1413},
            {113,2255},
            {114,21002},
            {115,2418},
            {116,2416},
            {117,1401},
            // Cerchio con elementale terra che si spappola dentro
            {118,InvalidIcon},
            {119,2287},
            {120,1411},
            {121,2252},
            {122,2253},
            {123,2262},
            {124,2258},
            {125,1407},
            {126,2254},
            {127,1409},
            {128,1410},
            {129,1408},
            // Mano con sopra teschio che "prende fuoco"
            {130,InvalidIcon},
            {131,2414},
            {132,2275},
            {133,20493},
            // Evoca ombra
            {134,InvalidIcon},
            // Palmo con simbolo neve
            {135,InvalidIcon},
            {136,2415},
            {137,20490},
            {138,2260},
            {139,2261},
            {140,2417},
            {141,2296},
            {142,2259},
            // CHECKPOINT: Cerchio con teschio
            {143,2278},
            {144,20480},
            {145,20486},
            {146,2277},
            {147,2251},
            {148,2297},
            {149,1245},
            {150,1246},
            // Fiamma figa
            {151,InvalidIcon},
            {152,1271},
            {153,1272},
            // Goccia d'acqua
            {154,InvalidIcon},
            // Vento
            {155,InvalidIcon},
            {156,21010},
            {157,21009},
            {158,21011},
            {159,21007},
            {160,20995},
            {161,21000},
            {162,20992},
            {163,20993},
            {164,20994},
            {165,21005},
            {166,20997},
            {167,20996},
            {168,20998},
            {169,20738},
            {170,21004},
            {171,21008},
            {172,21003},
            {173,20741},
            {174,21001},
            {175,20483},
        };
        #endregion
    }
}
